package com.treasure.mall.zbqmallmanager.controller.life;

import com.treasure.mall.biz.dto.condition.BaseSearchCondition;
import com.treasure.mall.biz.dto.life.ShopBusinessDTO;
import com.treasure.mall.biz.dto.life.ShopSaveDTO;
import com.treasure.mall.biz.dto.life.ShopSearchCondition;
import com.treasure.mall.biz.entity.embeddable.ReceivingInfo;
import com.treasure.mall.biz.entity.life.ShopPO;
import com.treasure.mall.biz.entity.life.ShopReservationRecordsPO;
import com.treasure.mall.biz.service.core.life.LifeShopService;
import com.treasure.mall.biz.service.core.user.MerchantService;
import com.treasure.mall.biz.vo.life.ShopBusinessVO;
import com.treasure.mall.zbq.common.base.ApiResult;
import com.treasure.mall.zbq.common.constant.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * <p> 本地生活
 * +---------------------------------------------------------
 * * @package: com.treasure.mall.zbqmallmanager.controller.life
 * * @author: guomw (guomwchen@foxmail.com)
 * * @date: 2020/10/13 5:21 下午
 * * @since: V1.0.0
 * +---------------------------------------------------------
 * 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的
 *
 */
@Controller
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    private LifeShopService shopService;
    @Autowired
    private MerchantService merchantService;

    /**
     * 门店列表
     *
     * @param pageIndex
     * @param model
     * @return
     */
    @GetMapping("/list")
    public String list(
            @ModelAttribute("condition") ShopSearchCondition condition,
            @RequestParam(required = false, defaultValue = "1") int pageIndex,
            Model model
    ) {
        Page<ShopPO> pos = shopService.findAll(pageIndex, Constant.DEFAULT_PAGE_SIZE, condition);
        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("totalRecords", pos.getTotalElements());
        model.addAttribute("totalPages", pos.getTotalPages());
        model.addAttribute("list", pos.getContent());
        return "life/shop_list";
    }


    @GetMapping("/list/records")
    public String listByRecords(
            @ModelAttribute("condition") BaseSearchCondition condition,
            @RequestParam(required = false, defaultValue = "1") int pageIndex,
            Model model
    ) {
        Page<ShopReservationRecordsPO> pos = shopService.findAllRecord(pageIndex, Constant.DEFAULT_PAGE_SIZE, condition);
        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("totalRecords", pos.getTotalElements());
        model.addAttribute("totalPages", pos.getTotalPages());
        model.addAttribute("list", pos.getContent());
        return "life/shop_record";
    }

    /**
     * 业务范围
     *
     * @param model
     * @return
     */
    @GetMapping("/list/scope")
    public String listByScope(Model model) {
        model.addAttribute("list", shopService.listByScope());
        return "life/shop_scope";
    }

    /**
     * 业务标签
     *
     * @param model
     * @return
     */
    @GetMapping("/list/tags")
    public String listByTags(Model model) {
        List<ShopBusinessVO> list = shopService.listByTags();
        list.add(0, new ShopBusinessVO("掌眼"));
        list.add(1, new ShopBusinessVO("修复"));
        model.addAttribute("list", list);
        return "life/shop_tags";
    }

    /**
     * 保存业务标签
     *
     * @param dto
     * @return
     */
    @PostMapping("/list/tags")
    @ResponseBody
    public ApiResult listByTags(ShopBusinessDTO dto) {
        shopService.saveTags(dto);
        return ApiResult.ok();
    }

    /**
     * 保存经营类目
     *
     * @param dto
     * @return
     */
    @PostMapping("/list/scope")
    @ResponseBody
    public ApiResult listByScope(ShopBusinessDTO dto) {
        shopService.saveScope(dto);
        return ApiResult.ok();
    }


    /**
     * 编辑门店
     *
     * @param shopId
     * @param model
     * @return
     */
    @GetMapping("/edit")
    public String edit(@RequestParam(required = false, defaultValue = "0") Long shopId, Model model) {
        ShopPO shopPO = shopService.findById(shopId);
        List<ShopBusinessVO> scopes = shopService.listByScope();
        List<ShopBusinessVO> tags = shopService.listByTags();
        if (shopPO != null) {
            scopes.forEach(item -> {
                if (shopPO.getBusinessScopes() != null && shopPO.getBusinessScopes().stream().filter(r -> r.getId().equals(item.getId())).findFirst().orElse(null) != null) {
                    item.setSelected(true);
                }
            });

            tags.forEach(item -> {
                if (shopPO.getBusinessTags() != null && shopPO.getBusinessTags().stream().filter(r -> r.getId().equals(item.getId())).findFirst().orElse(null) != null) {
                    item.setSelected(true);
                }
            });
        }
        model.addAttribute("shopId", shopId);
        model.addAttribute("shop", shopPO);
        model.addAttribute("tags", tags);
        model.addAttribute("scopes", scopes);
        return "life/shop_edit";
    }

    /**
     * 编辑门店信息
     *
     * @param dto
     * @return
     */
    @PostMapping("/edit")
    @ResponseBody
    public ApiResult edit(ShopSaveDTO dto, ReceivingInfo address) {
        dto.setShopAddress(address);
        if (dto.getOnlineMerchantId() != null && !merchantService.exists(dto.getOnlineMerchantId())) {
            return ApiResult.error("关联的商城店铺ID不存在");
        }
        Long shopId = shopService.saveShop(dto);
        if (shopId > 0) {
            return ApiResult.ok(shopId);
        }
        return ApiResult.error("保存失败");
    }

    /**
     * 冻结或启用门店
     *
     * @param shopId
     * @return
     */
    @PostMapping("/disabledToggle")
    @ResponseBody
    public ApiResult disabledToggle(Long shopId) {
        shopService.disabledToggle(shopId);
        return ApiResult.ok();
    }


}
